import { http } from "../http";
import {
  mockGetRepairWorkOrderList,
  mockAddOrUpdateRepairWorkOrder,
  mockDeleteRepairWorkOrder,
  mockGetRepairWorkOrderById,
} from "../mock/repairWorkOrder";
import {
  RepairWorkOrderType,
  AddOrUpdateRepairWorkOrderQueryType,
  GetRepairOrdersPageType,
  RepairWorkOrderStatus,
  UpdateRepairOrderStatusQueryType,
} from "../types/repairWorkOrder";

const baseUrl = "/api/dms/repairOrders";

// 控制是否使用Mock API的全局变量
const USE_MOCK = false; // 设置为 true 使用Mock，false 使用真实API

// 获取报修工单列表
export const getRepairOrdersPageAPI = (params: GetRepairOrdersPageType) => {
  if (USE_MOCK) {
    return mockGetRepairWorkOrderList().then((data) => data.data);
  }
  return http.post(`${baseUrl}/getRepairOrdersPage`, params);
};

// 添加或编辑报修工单
export const submitRepairOrderAPI = (
  params: AddOrUpdateRepairWorkOrderQueryType
) => {
  if (USE_MOCK) {
    return mockAddOrUpdateRepairWorkOrder(params).then((data) => data.data);
  }
  return http.post(`${baseUrl}/submitRepairOrder`, params);
};

// 删除报修工单
export const deleteRepairOrderAPI = (id: string) => {
  if (USE_MOCK) {
    return mockDeleteRepairWorkOrder(id).then((data) => data.data);
  }
  return http.delete(`${baseUrl}/deleteRepairOrder/${id}`);
};

// 根据ID获取报修工单详情
export const getRepairOrderByIdAPI = (id: string) => {
  if (USE_MOCK) {
    return mockGetRepairWorkOrderById(id).then((data) => data.data);
  }
  return http.get(`${baseUrl}/getRepairOrderById${id}`);
};

// 提交设备保修工单处理结果
export const updateRepairOrderStatusAPI = (
  params: UpdateRepairOrderStatusQueryType
) => {
  return http.put(`${baseUrl}/updateRepairOrderStatus/${params.id}`, {
    status: params.status,
    endTime: params.endTime,
    processingNote: params.processingNote,
    maintainerCode: params.maintainerCode,
  });
};
